﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;

using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.TemplateWizard;

namespace EntityCenterSiteLib
{
    public class MyWizard : IWizard
    {
        public string SharePointUrl; 
        public bool IsSandboxed = false;
        String fileName = Path.Combine(Path.GetTempPath(), "CenterSiteEntityDetails.txt");
        String CenterSiteVSTemplateExe = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)+@"\CenterSiteVSTemplateWizard.exe";


        public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
        {
           
        }

        /* this method will run on last step of template code 
         * we want to put sitreURL for debugging puprose and 
         * we want to add propery saying that this is not sandbox solution */

        public void ProjectFinishedGenerating(EnvDTE.Project project)
        {
            Microsoft.VisualStudio.OLE.Interop.IServiceProvider dTE = project.DTE as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
            ServiceProvider provider2 = new ServiceProvider(dTE);
            ISharePointProject project2 = (provider2.GetService(typeof(ISharePointProjectService)) as ISharePointProjectService).Convert<EnvDTE.Project, ISharePointProject>(project);

            if (project2 != null)
            {
                string str = Path.GetDirectoryName(project2.FullPath) + @"\";
                project2.SiteUrl = new Uri(SharePointUrl);
                project2.IsSandboxedSolution = IsSandboxed;

            }
            project2.Synchronize();

        }

        public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
        {
                
        }

        public void RunFinished()
        {
           
        }

        /* Below code will display the template Wizards */
        /* Replacing parameters in generated code are case sensitive and should be the same name */
        /* example     replacementsDictionary.Add("$entityNameproperty$", entityNameproperty); */

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
             string entityNameproperty;
             string entityNameSpaceproperty;
             string lobSystempInstanceroperty;
             string specificFinderproperty;
             string Finderproperty;
             string entityfieldsroperty=null;
             string sitedefid;

             
             SharepointForm form = new SharepointForm();
             form.ShowDialog();
             SharePointUrl = form.sharepointURL;

             String args; 
             args = fileName+"  "+ SharePointUrl;           

            /* pass the sharepoint url and filename to the exe */
            Process process = Process.Start(CenterSiteVSTemplateExe, args);
            process.WaitForExit();

            String[] str = File.ReadAllLines(fileName);
            int j = 0;

            sitedefid = str[j++].ToString();
            replacementsDictionary.Add("$sitedefid$", sitedefid);

            entityNameproperty = str[j++].ToString();
            replacementsDictionary.Add("$entityNameproperty$", entityNameproperty);

            entityNameSpaceproperty = str[j++].ToString();
            replacementsDictionary.Add("$entityNameSpaceproperty$", entityNameSpaceproperty);

            lobSystempInstanceroperty = str[j++].ToString();
            replacementsDictionary.Add("$lobSystempInstanceroperty$", lobSystempInstanceroperty);

            specificFinderproperty = str[j++].ToString();
            replacementsDictionary.Add("$specificFinderproperty$", specificFinderproperty);

            Finderproperty = str[j++].ToString();
            replacementsDictionary.Add("$Finderproperty$", Finderproperty);

            entityfieldsroperty = null;

            for (int i = j; i < str.Length; i++)
            {
                if(str[i].CompareTo(String.Empty) !=0)
                    entityfieldsroperty += str[i].ToString() + System.Environment.NewLine;
            }

            replacementsDictionary.Add("$entityfieldsroperty$", entityfieldsroperty);
         
       
        }
       

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}
